package com.kuaimacode.kframework.agent.controller.user;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.services.agent.SysAgentService;
import com.kuaimacode.kframework.agent.services.user.UserService;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.component.ComponentUtil;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.exception.ErrorEnum;
import com.kuaimacode.kframework.exception.ServerInternalErrorException;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgent;
import com.kuaimacode.kframework.mybatis.models.user.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Optional;

/**
 * @author qinyong
 * @Title:
 * @Package com.kuaimacode.kframework.admin.controller.user
 * @Description: created by IntelliJ IDEA
 * @date 2019-04-05 11:35
 */
@Controller
@RequestMapping({"/partner"})
public class PartnerController {

    private static String PAGE_FILE_NAME = "partner/";
    @Autowired
    private SysAgentService agentService;
    @Autowired
    private UserService userServices;
    @Autowired
    private ComponentUtil componentUtil;

    @RequestMapping(value = {"/index"})
    public String partner(@RequestParam(value = "pageNum", defaultValue = PageConstants.PAGE_NUM + "") int pageNum,
                          @RequestParam(value = "pageSize", defaultValue = PageConstants.PAGE_SIZE + "") int pageSize,
                          ModelMap model, User searchUser) {
        PageInfo<User> pageInfo = userServices.findPartnerPage(pageNum, pageSize, searchUser);
        model.put("pageInfo", pageInfo);
        model.put("searchUser", searchUser);
        return PAGE_FILE_NAME + "index";
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String editPartner(@PathVariable String id, ModelMap map) {
        User user = userServices.getById(id);
        Optional.ofNullable(user)
                .ifPresent(detail ->
                        Optional.ofNullable(detail.getPartnerAvatar())
                                .filter(StringUtils::isNotBlank)
                                .ifPresent(url -> user.setPartnerAvatar(componentUtil.convertImgUrl(url)))
                );
        map.put("user", user);
        //获取当前分站 总站的提成比例
        SysAgent agent = agentService.getById(SessionUtil.getAgentId());
        map.put("platRate",agent.getProportion());
        map.put("maxRate",new BigDecimal(100).subtract(agent.getProportion()));
        return PAGE_FILE_NAME + "form";
    }


    @RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult editPartner(User user) {
        return JsonResult.success(userServices.editPartner(user));
    }

    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public String detail(@PathVariable String id, ModelMap map) {
        User user = userServices.getById(id);
        Optional.ofNullable(user)
                .ifPresent(detail ->
                        Optional.ofNullable(detail.getPartnerAvatar())
                        .filter(StringUtils::isNotBlank)
                        .ifPresent(url -> user.setPartnerAvatar(componentUtil.convertImgUrl(url)))
                );
        map.put("user", user);
        return PAGE_FILE_NAME + "detail";
    }

    @PostMapping("agree/{id}")
    @ResponseBody
    public JsonResult agree(@PathVariable Long id, BigDecimal scale) {
        if (scale == null) {
            throw new ServerInternalErrorException(ErrorEnum.PARAM_VALID_ERROR);
        }
        userServices.agreePartner(id, scale);
        return JsonResult.success();
    }

    @PostMapping("reject/{id}")
    @ResponseBody
    public JsonResult reject(@PathVariable Long id, @RequestParam String reason) {
        userServices.rejectPartner(id, reason);
        return JsonResult.success();
    }
}
